package com.bupt.ilink.mapper.innovationPlaza;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bupt.ilink.entity.innovationPlaza.CompetitionMetadata;
import com.bupt.ilink.entity.innovationPlaza.DTO.CompetitionResponse;
import org.apache.ibatis.annotations.*;
import java.math.BigInteger;
import java.util.List;

@Mapper
public interface CompetitionMapper extends BaseMapper<CompetitionMetadata> {

    @Select("SELECT icm.competition_id, icm.name, GROUP_CONCAT(it.name) AS tags, icm.description, " +
            "icm.like_num, icm.share_num, icm.look_num, icm.publisher_time, icm.status " +
            "FROM innovation_competition_metadata icm " +
            "LEFT JOIN innovation_connect_tags ict ON icm.competition_id = ict.project_id " +
            "LEFT JOIN innovation_tags it ON ict.tag_id = it.tag_id " +
            "GROUP BY icm.competition_id, icm.name, icm.description, icm.like_num, icm.share_num, " +
            "icm.look_num, icm.publisher_time, icm.status")
    List<CompetitionResponse> getAllCompetitionDetails();
    @Select("SELECT icm.competition_id, icm.name, icm.description, icm.like_num, icm.share_num, icm.look_num, " +
            "icm.publisher_time, icm.status " +
            "FROM innovation_competition_metadata icm " +
            "ORDER BY ${sortBy} DESC")
    IPage<CompetitionResponse> getAllCompetitions(Page<?> page, @Param("sortBy") String sortBy);

    @Select("SELECT icm.competition_id, icm.name, icm.description, icm.like_num, icm.share_num, icm.look_num, " +
            "icm.publisher_time, icm.status " +
            "FROM innovation_competition_metadata icm " +
            "ORDER BY ${sortBy} DESC")
    List<CompetitionResponse> getAllCompetitionsNopage(@Param("sortBy") String sortBy);

    @Update("UPDATE innovation_competition_metadata SET like_num = like_num - 1 WHERE competition_id = #{competitionId}")
    int decrementLikeNum(@Param("competitionId") Long competitionId);

    @Update("UPDATE innovation_competition_metadata SET like_num = like_num + 1 WHERE competition_id = #{competitionId}")
    int incrementLikeNum(@Param("competitionId") Long competitionId);

    @Update("UPDATE innovation_competition_metadata SET look_num = look_num + 1 WHERE competition_id = #{competitionId}")
    int incrementLookNum(@Param("competitionId") Long competitionId);

    @Update("UPDATE innovation_competition_metadata SET share_num = share_num + 1 WHERE competition_id = #{competitionId}")
    int incrementShareNum(@Param("competitionId") Long competitionId);

 }
